var timeOut;

function getMyComplaintsTable(array){
	
	var a = jQuery.parseJSON(array);
	var even = "";
	var table = "<table class='myComplaintsTable'><tr>";
	
	table += "<th id='c.id'>" + a[0][0] + "</th>";
	table += "<th id='c.heading'>" + a[0][1] + "</th>";
	table += "<th id='c.date'>" + a[0][3] + "</th>";
	table += "<th id='applicant'>" + a[0][4] + "</th>";
	table += "<th id='c.priority'>" + a[0][5] + "</th>";
	table += "<th id='s.status'>" + a[0][6] + "</th>";

	table += "</tr>";
	
	for (var i = 1; i < a.length; i++){
		if (i % 2 == 1){
			even = "even";
		}
		else{
			even = "";
		}
		
		table += "<tr onclick='openForm(\""+ a[i][2] +"\")' class='complaint " + even + "' id='ow_" + i + "'>";
		for (var j = 0; j < a[0].length; j++){
			if(j != 2){
				table += "<td>" + a[i][j] + "</td>";
			}
		}
		table += "</tr><tr><td id='row_" + i.toString() + "' class='descriptionRow " + even + "' colspan='" + (a[0].length - 1) + "'>" + a[i][2] + "</td></tr>";
	}
	table += "</table>";
	return table;
}

function openForm(complaint){
	var url = window.location.pathname;
	var filename = url.substring(url.lastIndexOf('/')+1);
	var page = "window.location.href='"+filename+"jsp'";
	text = "<div class='comment bluebox'>"+complaint+"</div>";
	text += "<div class='bluebox' id='comment-form'>Lisa kommentaar:<br />" +
			"<textarea id='comment-area'></textarea><br />"+
			"<input type='button' value='Sisesta' /><br />"+
			"</div>"+
			"<div class='bluebox' id='complaint-settings'><table>"+
			"<tr><td>Kaebusega tegeleja</td>"+
            "<td><select>" +
            "<option selected>kasutaja</option>" +
            "</select>" +
            "</td></tr><tr>" +
            "<td>Prioriteet</td>" +
            "<td>" +
            "<select>" +
            	"<option>0</option>" +
            	"<option selected>1</option>" +
            	"<option>2</option>" +
            	"<option>3</option>" +
            	"<option>4</option>" +
            	"<option>5</option>" +
            	"</select>" +
            "</td>" +
       "</tr>" +
       "<tr>" +
       "<td>Grupp</td>" +
       		"<td>" +
       		"<select>" +
       		"<option selected></option>" +
       		"<option>Foo</option>" +
       		"<option>Bar</option>" +
       		"</select>" +
       		"</td>" +
       		"</tr>" +
       		"<tr>" +
       		"<td colspan='2'>";
	if (filename == "unassigned.jsp") {
		text += "<input type='button' value='M&auml;&auml;ra' />" +
		"<input type='button' value='Kustuta kaebus' />" +
		"</td>" +
		"</tr>" +
		"</table></div>";
	} else if (filename == "assigned.jsp"){
		text += "<input type='button' value='V&otilde;ta m&auml;&auml;ramatuks tagasi' />" +
		"<input type='button' value='Kustuta kaebus' />" +
		"</td>" +
		"</tr>" +
		"</table></div>";
	} else if (filename == "mycomplaints.jsp"){
		text += "<input type='button' onclick="+page+" value='Sulge kaebus' />" +
		"</td>" +
		"</tr>" +
		"</table></div>";
	}
       			
	document.getElementById("myComplaintsTableArea").innerHTML = text;
}

function showDescriptionOnHover(){
	$('.complaint').hover(function() {
        var id = "r" + $(this).attr('id');
        timeOut = setTimeout(function(){$('#' + id).show('fast');}, 800);
    },
    function() {
    	clearTimeout(timeOut);
    	var id = "r" + $(this).attr('id');
        $('#' + id).hide("fast");
    });
}

function makeTableSortable(){
	$(".myComplaintsTable th").click(
			function(){
				getMyComplaintsFromDB("'" + $(this).attr("id") + "'");
				makeTableSortable();
			}
	);
}